/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.posServices;

import com.openbrew.openbrewrf.POSSale;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.AbstractFacade;
import tools.POSSaleTools;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.possale")
public class POSSaleFacadeREST extends AbstractFacade<POSSale> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public POSSaleFacadeREST() {
        super(POSSale.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public POSSale create(@WebParam(name = "possale") POSSale entity) {
        POSSaleTools psTools = new POSSaleTools();
        POSSale newEntity = psTools.doPOSSaleInsertActions(em, entity);
        newEntity = psTools.doLoyaltyItemsOnInsert(em, newEntity);
        newEntity = super.create(newEntity);
        return newEntity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(POSSale entity) {
        POSSaleTools psTools = new POSSaleTools();
        final POSSale newEntity = psTools.doLoyaltyItemsOnUpdate(em, entity);
        super.edit(newEntity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public POSSale find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<POSSale> findAll() {
        return super.findAll();
    }

    @GET
    @Path("findAllOnDate/{date}")
    @Produces({"application/xml", "application/json"})
    public List<POSSale> findAllOnDate(@PathParam("date") Date date) {
        Calendar calStart = Calendar.getInstance();
        calStart.setTime(date);
        calStart.set(Calendar.HOUR_OF_DAY, 0);
        calStart.set(Calendar.MINUTE, 0);
        calStart.set(Calendar.SECOND, 0);
        final Date startDate = calStart.getTime();
        Calendar calStop = Calendar.getInstance();
        calStop.setTime(date);
        calStop.set(Calendar.HOUR_OF_DAY, 23);
        calStop.set(Calendar.MINUTE, 59);
        calStop.set(Calendar.SECOND, 59);
        final Date stopDate = calStop.getTime();
        //"SELECT s FROM POSSale s WHERE s.saleComplete = 'true' AND s.tabPaidTime >= ?1 AND s.tabPaidTime <= ?2", POSSale.class);
        
        TypedQuery<POSSale> query = getEntityManager().createQuery(
                "SELECT s FROM POSSale s WHERE s.saleComplete = 'true' AND s.tabPaidTime >= ?1 AND s.tabPaidTime <= ?2", POSSale.class);
        query.setParameter(1, startDate, TemporalType.TIMESTAMP);
        query.setParameter(2, stopDate, TemporalType.TIMESTAMP);
        List<POSSale> sales = query.getResultList();
        return sales;
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<POSSale> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<POSSale> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}
